import axios from 'axios';

// API基础配置
export const API_BASE_URL = 'http://localhost:6161/api';

// 创建axios实例
export const apiClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
});

// 请求拦截器
apiClient.interceptors.request.use(
  (config) => {
    console.log('发送请求:', config);
    return config;
  },
  (error) => {
    console.error('请求错误:', error);
    return Promise.reject(error);
  }
);

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => {
    console.log('收到响应:', response);
    return response;
  },
  (error) => {
    console.error('响应错误:', error);
    return Promise.reject(error);
  }
);

// API端点常量
export const API_ENDPOINTS = {
  LOVE_MASTER_SSE: '/love_master/sse',
  AGENT_SSE: '/agent/sse',
} as const;

// 消息类型定义
export interface Message {
  id: string;
  content: string;
  sender: 'user' | 'ai';
  timestamp: Date;
  chatId: string;
}

// 创建消息对象的工具函数
export function createMessage(
  content: string,
  sender: 'user' | 'ai',
  chatId: string
): Message {
  return {
    id: Date.now().toString() + Math.random().toString(36).substr(2, 9),
    content,
    sender,
    timestamp: new Date(),
    chatId,
  };
}